package nl.hanze.FlitsNieuws.Activities;

import android.content.SharedPreferences;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.os.Bundle;
import android.preference.EditTextPreference;
import android.preference.PreferenceActivity;
import android.view.Menu;
import android.view.MenuItem;
/**
 * Deze klasse beheert de instellingen van de app, gebruikersnaam en wachtwoord worden bij elk
 * verzonden nieuwsitem meegestuurd om het verzoek te authenticeren. De hostname bevat de URL
 * waar de data heengestuurd wordt.
 *  
 * @author alex
 */
public class SettingsActivity extends PreferenceActivity implements OnSharedPreferenceChangeListener {

	// gebruikersnaam
    public static final String STRING_NAME_PREFERENCE = "name";
    // wachtwoord
    public static final String STRING_PASSWORD_PREFERENCE = "password";
    // hostname van de networkservice
    public static final String STRING_POSTADDRESS_PREFERENCE = "postaddress";
    private EditTextPreference namePreference;
    private EditTextPreference passWordPreference;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.preferences);
        namePreference = (EditTextPreference) getPreferenceScreen().findPreference(STRING_NAME_PREFERENCE);
        passWordPreference = (EditTextPreference) getPreferenceScreen().findPreference(STRING_PASSWORD_PREFERENCE);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_settings, menu);
        return true;
    }

    @Override
    public void onResume() {
        super.onResume();
        //Laat de instellingen zien
        namePreference.setSummary(namePreference.getText());
        if (!passWordPreference.getText().equals("")) {
            passWordPreference.setSummary(R.string.PasswordIsSet);
        } else {
            passWordPreference.setSummary(R.string.noPassword);
        }

        // Set up a listener whenever a key changes            
        getPreferenceScreen().getSharedPreferences().registerOnSharedPreferenceChangeListener(this);
    }

    @Override
    public void onPause() {
        getPreferenceScreen().getSharedPreferences().unregisterOnSharedPreferenceChangeListener(this);
        super.onPause();
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        }
        return super.onOptionsItemSelected(item);
    }

    public void onSharedPreferenceChanged(SharedPreferences sharedPreferences,
            String key) {
        if (key.equals(STRING_NAME_PREFERENCE)) {
            namePreference.setSummary(namePreference.getText());
        } else if (key.equals(STRING_PASSWORD_PREFERENCE)) {
            if (!passWordPreference.getText().equals("")) {
                passWordPreference.setSummary(R.string.PasswordIsSet);
            } else {
                passWordPreference.setSummary(R.string.noPassword);
            }
        }
    }
}
